package auth;

import frame.service.standard.MiayRuntimeException;
import page.Pages;

import java.util.Iterator;
import java.util.List;

/**
 * 权限组信息，客户会话认证后会获取权限组信息留存至会话中
 * */
public class FuncPermission {
    private List<Pages.MenuLevel1> menus;

    public List<Pages.MenuLevel1> getMenus() {
        return menus;
    }

    public void setMenus(List<Pages.MenuLevel1> menus) {
        this.menus = menus;
    }

    public boolean checkPermission(String sceneId){
        String[] scenes = sceneId.split(":");
        if(scenes.length!=3){
            throw new MiayRuntimeException("930004");
        }
        String requestMenuL1=scenes[0], requestMenuL2=scenes[1], requestOperation=scenes[2];

        //一级菜单权限判断
        Pages.MenuLevel1 menuLevel1 = null;
        for(Pages.MenuLevel1 tmp: menus){
            if(requestMenuL1.equals(tmp.getId())){
                menuLevel1 = tmp;
            }
        }

        //一级菜单权限不足 || 二级菜单权限不足
        if(menuLevel1==null || menuLevel1.getMenus()==null){
            throw new MiayRuntimeException("930006");
        }

        boolean pass = false;
        for(Pages.MenuLevel2 m2: menuLevel1.getMenus()){
            if(requestMenuL2.equals(m2.getId())){
                //匹配到二级菜单，继续判断业务操作权限
                Iterator<Pages.Operation> iterator = m2.getOperations().iterator();
                while(iterator.hasNext()){
                    Pages.Operation next = iterator.next();
                    if(requestOperation.equals(next.getId())){
                        //找到对应的业务操作，具备业务操作权限
                        pass = true;
                    }
                }
            }
        }

        if(!pass){
            throw new MiayRuntimeException("930006");
        }

        return true;
    }
}
